package com.blb.socket.demo1;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * 客户端
 */
public class MyClient {

    /**
     * 发送消息
     * @param ip
     * @param port
     * @param message
     */
    public void sendMessage(String ip,int port,String message){
        //创建Socket对象
        try(Socket socket = new Socket(ip,port);
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream())) {
            //获得输出流，向服务器发送消息
            oos.writeUTF(message);
            //清空缓冲区
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        MyClient myClient = new MyClient();
        myClient.sendMessage("192.168.1.104",8888,"Hello!!服务器");
    }
}
